package com.mipo.company.flytek.jxt.authourity.beilun;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.net.URLDecoder;

import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.alibaba.fastjson.JSONObject;

public class Main {
	public static Logger log = Logger.getLogger(Main.class);
	
	public static void main(String[] args) throws Exception {
		//PropertyConfigurator.configure("G:/project/LunaMavneJdk7A/src/main/resources/log4j.properties");
		//log.info("xxxx");
		read();
		
	}
	
	public static void read() throws Exception {
		FileReader fr = new FileReader("G:\\bbbb.txt");
		BufferedReader br=new BufferedReader(fr);
		String s=br.readLine();	//创建变量s用于存储从文件中读取的第一行数据
		while(s!=null){			//判断s变量是否接收到数据
			String[] data = s.split("##");
			if (data!=null && data.length<3) {
				s=br.readLine();
				continue;
			}
			String login_name = data[0];
			String school_name = data[1];
			String user_name = data[2];
//			if ("gelh3".equals(login_name)) {
//				System.out.println("xxxx");
//			}
			//System.out.println(s);
			
			String res = queryStatus(login_name, school_name, user_name);
			if (!"".equals(res)) {
				say(res);
			}
			
			s=br.readLine();
		}
		br.close();
	}
	
	public static void say(String res)throws Exception{
		String filePath = "D:/shoplist.txt";
		File file = new File(filePath);
		OutputStream os = new FileOutputStream(file, true);
		os.write(res.getBytes("UTF-8"));
		os.write("\r\n".getBytes("UTF-8"));
		os.flush();
		os.close();
	}
	
	
	
	// 查询状态
		public static String queryStatus(String loginName, String school_name, String user_name) throws Exception {
			boolean isTimeout = false;
			int times = 1;
			String url = "http://nbbl.book.changyan.cn/index.php?c=home&a=getUserCardState&loginName="+loginName+"&appName=rrt_nbbl";
			String resp = "";
			while (!isTimeout) {
				System.out.println("次数为："+times);
				if (times <= 5) {
					try{
						resp = excuteGetRequest(url);
						if (!"".equals(resp)){
							isTimeout = true;
						}
					}catch(Exception e){
						e.printStackTrace();
					}finally{
						times ++;
					}
				}
				
				
			}
			if("".equals(resp)){
				return "";
			}
			
			JSONObject jo = (JSONObject) JSONObject.parse(resp);
			String status = jo.getString("status");
			String msg = jo.getString("msg");
			//System.out.println(status);
			//System.out.println(msg);
			if ("0".equals(status)) {
				//INSERT INTO ledc_jxtauth.ta_book_user(user_name, login_name,book_num,warrant_time,organization,role_enName) VALUES ('林老师', 'js_2', '80',  now(), '希望小学', 'teacher');
				StringBuilder sb = new StringBuilder();
				sb.append("INSERT INTO ledc_jxtauth.ta_book_user(user_name, login_name,book_num,warrant_time,organization,role_enName) VALUES ('");
				sb.append(user_name).append("', '").append(loginName).append("', '4',  now(), '").append(school_name).append("', 'teacher');");
				System.out.println(loginName);
				//log.info(sb);
				return sb.toString();
			}
			return "";
			
		}
		
		public static String excuteGetRequest(String url) throws Exception {
			//System.out.println("请求url："+url);
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(httpGet); // 4.3版本不设置超时的话，一旦服务器没有响应，等待时间N久(>24小时)。
			HttpEntity entity = response.getEntity(); // 返回报文实体
			StatusLine status = response.getStatusLine();
			//System.out.println("返回的状态码为：" + status); // 具体返回码(200)为status.getStatusCode();
			String ret = EntityUtils.toString(entity); // 将返回报文实体转换为字符串
			//System.out.println("返回的报文为：" + URLDecoder.decode(ret,"UTF-8"));
			EntityUtils.consume(entity); // 释放是否可以去掉？
			response.close(); // 关闭资源,可以写在finally里
			httpGet.releaseConnection(); // 释放资源
			httpClient.close(); // 关闭资源
			return URLDecoder.decode(ret,"UTF-8");
		}
	
}
